/**
 * Created by yujia on 2017/11/29.
 */

$(document).ready(function () {
    var result = {
        classify: $.cookie("classify"),
        userId: 123
    }
    $.ajax({
        type: 'POST',
        url: "/result/score.json",
        contentType: "application/json;",
        data: JSON.stringify(result),
        success: function (data) {
            $("#classify").html("<h3>" + $.cookie("classify") + "</h3>");

            if(data.totolQuestion == 0){
                alert("很抱歉,您还没有做过此类型试题!");
                $(location).attr("href","/views/review.html");
            }
            var score = Math.ceil(data.rightQuestion / data.totolQuestion * 100);
            var scoreStr = "您的分数: " + score + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            scoreStr +="<small>您答对了" + data.rightQuestion + "题 , 共" + data.totolQuestion + "题</small>";
            $("#score h4").html(scoreStr);
            var resultStr = "";
            for(var i=0;i<data.wrongResults.length;i++){
                resultStr += '<a href="#" class="list-group-item">';
                resultStr += '<h4 class="list-group-item-heading">';
                resultStr += data.wrongResults[i].title;
                resultStr += '</h4>';
                resultStr += '<p class="list-group-item-text">';
                if(data.wrongResults[i].rightAnswer == 1){
                    resultStr += "正确";
                }else if(data.wrongResults[i].rightAnswer == 0){
                    resultStr += "错误";
                }else{
                    resultStr += data.wrongResults[i].rightAnswer;
                }
                resultStr += '</p>';
                resultStr += '</a>';
            }
        
            $("#result").html(resultStr);
        }, error: function (xhr, type) {
            $(location).attr('href', '/views/result.html');
        }
    });
});